<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class OnlineListDataProvider extends CMiniyunActiveDataProvider {
    private $_list = array ();
    
    
    public function getData($refresh = false) {
        if (empty ( $this->_list ) || $refresh) {
            $this->model = new OnlineDevice ();
            $this->initData ( "(now()- updated_at)<=300" );             

            $ids = "";
                        foreach ( $this->data as $index => $item ) {
                $id = $item->user_id;
                if (strlen ( $ids ) > 0) {
                    $ids = $ids . ",";
                }
                $ids = $ids . $id;
            }
            
                        $userMate = new UserMeta ();
            $userMetaData = $userMate->getUserBaseMate ( $ids );
                        $usersHandler = new User ();
            $handlerServerRegistry = new ServerRegistry ();
            $handleUserDevice = new UserDevice ();
            
                        foreach ( $this->data as $index => $item ) {
                $appID = $item ["application_id"];
                $did = $item ["device_id"];
                $user = $usersHandler->findByPk($item["user_id"]);
                $online = new OnlineBase ();
                $online->name = $user ["user_name"];
                $online->id = $item ["id"];
                $online->userID = $user ["id"];
                $online->did = $did;
                $online->appid = $appID;
                $online->lastLoginTime = $item ["updated_at"];
                $this->_fillUserMeta ( $user ["id"], $online, $userMetaData );
                $apps = ServerRegistry::getApps ();
                $device = $handleUserDevice->findByPk ( $did );
                $online->appname = $apps ["$appID"];
                $online->deviceType = $device ["user_device_type"];
                $online->deviceName = $device ["user_device_name"];
                array_push ( $this->_list, $online );
            }
        }
        return $this->_list;
    }
    
    private function _fillUserMeta($key, &$item, $userMetaData) {
        foreach ( $userMetaData as $index => $userMetaItem ) {
            $meta_key = $userMetaItem->meta_key;
            $meta_value = $userMetaItem->meta_value;
            $user_id = $userMetaItem->user_id;
            if ($user_id == $key) {
                if ($meta_key == "avatar") {
                    $item->avatar = $meta_value;
                }
            }
            if (! isset ( $item->avatar )) {
                $item->avatar = Yii::app ()->params ["defaultAvatar"];
            }
        }
    }
}
?>